package com.luoxudong.phonenumber.ui.activities;

import android.app.ActivityGroup;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.ViewFlipper;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.logic.area.AreaFactory;
import com.luoxudong.phonenumber.logic.area.interfaces.IAreaManager;
import com.luoxudong.phonenumber.service.pool.manager.ThreadTaskObject;
import com.luoxudong.phonenumber.ui.widgets.CommonDialog;

public class MainActivity extends ActivityGroup {

	private ViewFlipper mainContainerLayout = null;
	private RadioGroup mainRadioGroup = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		mainContainerLayout = (ViewFlipper)findViewById(R.id.ll_main_container);
		mainRadioGroup = (RadioGroup)findViewById(R.id.main_radioGroup);
		mainRadioGroup.setOnCheckedChangeListener(onCheckedChangeListener);
		initDataAsync();
		ensureUI();
	}
	
	private void ensureUI()
	{
		Intent intent = new Intent(MainActivity.this, HomeActivity.class);
		launchActivity(intent);
	}
	
	public void launchActivity(Intent intent) {
		mainContainerLayout.removeAllViews();
		//mainContainerLayout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
		mainContainerLayout.addView(getLocalActivityManager().startActivity(
				intent.getComponent().getShortClassName()
						+ mainRadioGroup.getCheckedRadioButtonId(),
				intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))
				.getDecorView());
	}
	
	/** ActivityGroup加载新的子Activity的方法(创建新的) */
	public void launchNewActivityForResult(AbsSubActivity requestSubActivity,
			Intent intent, int requestCode) {
		intent.putExtra("requestCode", requestCode);
		mainContainerLayout
				.addView(getLocalActivityManager().startActivity(
						intent.getComponent().getShortClassName()
								+ mainRadioGroup.getCheckedRadioButtonId(),
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
		mainContainerLayout.setInAnimation(this, R.anim.push_left_in);
		mainContainerLayout.setOutAnimation(this, R.anim.remain);
		mainContainerLayout.showNext();
		((AbsSubActivity) getCurrentActivity())
				.setRequestSubActivity(requestSubActivity);
	}
	
	/** ActivityGroup加载新的子Activity的方法(创建新的) */
	public void launchNewActivity(Intent intent) {
		mainContainerLayout
				.addView(getLocalActivityManager().startActivity(
						intent.getComponent().getShortClassName()
								+ mainRadioGroup.getCheckedRadioButtonId(),
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
		
		mainContainerLayout.setInAnimation(this, R.anim.push_left_in);
		mainContainerLayout.setOutAnimation(this, R.anim.remain);
		mainContainerLayout.showNext();
	}
	
	public void gobackActivity()
	{
		if (mainContainerLayout == null || mainContainerLayout.getChildCount() <= 1)
		{
			finish();
			return;
		}
		
		mainContainerLayout.setInAnimation(this, R.anim.remain);
		mainContainerLayout.setOutAnimation(this, R.anim.push_right_out);
		mainContainerLayout.showPrevious();
		
		//mainContainerLayout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
		mainContainerLayout.removeViewAt(mainContainerLayout.getChildCount() - 1);
		
		/*if (mainContainerLayout.getChildCount() > 0)
		{
			mainContainerLayout.getChildAt(mainContainerLayout.getChildCount() - 1).setVisibility(View.VISIBLE);
		}*/
	}
	
	public void exitApp()
	{
		//退出
		CommonDialog.Builder builder = new CommonDialog.Builder(this);
		builder.setTitle(R.string.title_prompt);
		builder.setMessage(R.string.exit_confirm);
		builder.setPositiveButton(R.string.common_confirm, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});
		builder.setNegativeButton(R.string.common_cancel, null);
		builder.create().show();
	}
	
	private OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			Intent intent = null;
			switch (checkedId) {
			case R.id.rb_tabbar_home:
				intent = new Intent(MainActivity.this, HomeActivity.class);
				break;
			case R.id.rb_tabbar_search:
				intent = new Intent(MainActivity.this, SearchActivity.class);
				break;
			case R.id.rb_tabbar_app:
				intent = new Intent(MainActivity.this, AppActivity.class);
				break;
			case R.id.rb_tabbar_account:
				intent = new Intent(MainActivity.this, AccountActivity.class);
				break;
			default:
				break;
			}
			
			launchActivity(intent);
		}

	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
		{
			return getLocalActivityManager().getCurrentActivity().onKeyDown(keyCode, event);
			
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * 异步初始化数据
	 * 
	 */
	private void initDataAsync()
	{
		new ThreadTaskObject(){
			public void run() {
				IAreaManager areaManager = AreaFactory.getAreaManager();
				areaManager.initLocalAreaInfo(MainActivity.this);
			};
		}.start();
	}
	
}
